package NWPackage;

import java.util.ArrayList;
import java.util.List;

public class ProbabilityCell extends Cell {
	
	//ordered by index - A<B<C....????
	private List<String> candidates;
	private double probability;
	
	public ProbabilityCell() {
		super();
	}
	public ProbabilityCell(String agentName) {
		super(agentName);
	}
	public ProbabilityCell(String agentName,double probability) {
		super(agentName);
		this.probability = probability;
	}
	public ProbabilityCell(String agentName,double probability,List<String> candidates) {
		super(agentName);
		this.probability = probability;
		this.candidates = candidates;
	}
	public ProbabilityCell(ProbabilityCell probabilityCell) {
		this.candidates = probabilityCell.candidates;
		this.probability = probabilityCell.getProbability();
		this.setAgentName(probabilityCell.getAgentName());
		this.setIndex(probabilityCell.getIndex());
	}
	public boolean isEqualsPreferredOrder(PairPreferdCandidateCell preferredCell) {
		//compare between the preference from "preferredCell" and this cell's Candidates order
		//if the index of the preferred Candidate is bigger the the second Candidate - return Equal.
		
		boolean answer = false;
		String[] candidates = preferredCell.getCandidates();
		String preferredCandidate = preferredCell.getPrefered_C();
		
		int firstCandidateIndex = getIndexOf(candidates[0]);
		int secondCandidateIndex = getIndexOf(candidates[1]);
		
		if (preferredCandidate == candidates[0] && firstCandidateIndex > secondCandidateIndex) //edit > 
			answer = true;
		else if(preferredCandidate == candidates[1] && secondCandidateIndex > firstCandidateIndex)//edit > 
			answer = true;
		return answer;
	}
	public int getIndexOf(String candidateName) {
		int index =  this.candidates.indexOf(candidateName);
		return index;
	}
	public double getProbability() {
		return this.probability;
	}
	
	public String[] getCandidates()
	{
		if (this.candidates!=null)
			return (String[])this.candidates.toArray();
		return null;
	}
	
	public void setProbability(double newProb) throws Exception
	{
		if (newProb>=0 && newProb <=1)
			this.probability = newProb;
		else
			throw new Exception("probability must be between 0 and 1");
	}
	public void setCandidates(List<String> permutation) {
		this.candidates = permutation;
	}
	public ProbabilityCell clone()
	{
		return new ProbabilityCell(this);
	}
	//extract all posible queries from this cell 
	public List<PairPreferdCandidateCell> extractQueries() {
		List<PairPreferdCandidateCell> ans = new ArrayList<PairPreferdCandidateCell>();
		String[] cands = (String[])this.candidates.toArray();
		int index = 0;
		for (int i = 0; i < cands.length; i++) {
			for (int j = i+1; j < cands.length; j++) {
				PairPreferdCandidateCell found = new PairPreferdCandidateCell(this.getAgentName(),index, cands[i], cands[j]);
				//set the real vote from the Probability cell
				if (this.candidates.indexOf(cands[i]) > this.candidates.indexOf(cands[j]))
					found.setPrefered_C(cands[i]);
				else
					found.setPrefered_C(cands[j]);
				ans.add(found);
				index++;
			}
		}
		return ans;
	}
	public String candidatesToString() {
		String ans = "";
		for (String cand : candidates) {
			ans+= cand + ",";
		}
		return ans.substring(0, ans.length()-1);
	}
	
	public String printCell(){
		String s = candidates.toString() + " " + this.probability;
		return s;
	}

}
